<%@page import="it.apuliait.service.DocumentoLocalServiceUtil"%>
<%@page import="com.liferay.portal.kernel.repository.model.FileEntry"%>
<%@page import="com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil"%>
<%@page import="it.apuliait.service.TrasmittenteLocalServiceUtil"%>
<%@page import="it.apuliait.model.Trasmittente"%>
<%@page import="com.liferay.portlet.documentlibrary.model.DLFolder"%>
<%@page import="com.liferay.portal.kernel.util.WebKeys"%>
<%@page import="com.liferay.portal.theme.ThemeDisplay"%>
<%@page import="com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil"%>
<%@page import="com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil"%>
<%@page import="com.liferay.portlet.documentlibrary.model.DLFileEntry"%>
<%@page import="it.apuliait.model.DocumentoXml"%>
<%@page import="it.apuliait.service.DocumentoXmlLocalServiceUtil"%>
<%@page import="it.apuliait.model.Documento"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="javax.portlet.PortletURL"%>
<%@page import="javax.portlet.ActionRequest"%>
<%@ taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>

<script src="http://cdn.alloyui.com/3.0.0/aui/aui-min.js"></script>
<link href="http://cdn.alloyui.com/3.0.0/aui-css/css/bootstrap.min.css" rel="stylesheet"></link>

<portlet:defineObjects />

<div style="display: inline-flex;">
	
			<form method="post" action="<portlet:renderURL>
				<portlet:param name="nuovaFattura" value="nuovaFattura" />
				<portlet:param name="scelta" value="NUOVO_DOCUMENTO" />
				</portlet:renderURL>">
				<input type="submit" value="Nuovo Documento"/>
			</form>			
			<p>&nbsp;</p><p>&nbsp;</p>
			<form method="post" action="<portlet:renderURL><portlet:param name="scelta" value="ELENCO_RICEVENTE" /></portlet:renderURL>">
				<input type="submit" value="Elenco Riceventi"/>
			</form>
			<p>&nbsp;</p><p>&nbsp;</p>
			<form method="post" action="<portlet:renderURL><portlet:param name="scelta" value="REGISTRA_IMPRESA" /></portlet:renderURL>">
				<input type="submit" value="Dati del Trasmittente"/>
			</form>
			<p>&nbsp;</p><p>&nbsp;</p>
			<form method="post" action="<portlet:renderURL><portlet:param name="trasmittenteId" value="1" /><portlet:param name="scelta" value="TRASMETTI_FILE" /></portlet:renderURL>">
				<input type="submit" value="Trasmetti File" />
			</form>
</div>

<p><b>ELENCO DOCUMENTI</b></p>

<% 
ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
List<Documento> list_documento = (List<Documento>)renderRequest.getAttribute("list_documento");

PortletURL iteratorURL = renderResponse.createRenderURL();
List<Documento> displayList = new ArrayList<Documento>();
pageContext.setAttribute("list_documento", list_documento);

%>

<form name="modulo">
	<liferay-ui:search-container delta="10" emptyResultsMessage="Nessun Documento" iteratorURL="<%= iteratorURL %>" >
	<%
	
	if(list_documento!=null && list_documento.size()>0 ){	  
		displayList = list_documento.subList(searchContainer.getStart(), list_documento.size());
	}
	if( searchContainer.getEnd() < list_documento.size()){
		displayList=list_documento.subList(searchContainer.getStart(), searchContainer.getEnd());
	}
	
	pageContext.setAttribute("displayList", displayList);
	
	%>
	<liferay-ui:search-container-results results="<%= displayList %>" total="<%= list_documento.size() %>" />
	     <liferay-ui:search-container-row className="it.apuliait.model.Documento" keyProperty="documentoId" modelVar="documento" >
	       	<liferay-ui:search-container-column-text name="Documento" >
	       		 <a href="<portlet:renderURL>
	       		 				<portlet:param name="documentoId" value="<%= String.valueOf(documento.getDocumentoId()) %>" />
	       		 				<portlet:param name="scelta" value="MODIFICA_DOCUMENTO" />
	       		 		</portlet:renderURL>"><%= documento.getNomeDocumento() %></a> 
	       	</liferay-ui:search-container-column-text>
	       	<liferay-ui:search-container-column-text name="Anno">
	       		<p align="center"><%= documento.getDataDocumento().split("-")[0] %></p>
	       	</liferay-ui:search-container-column-text>
	       	<liferay-ui:search-container-column-text name="Tipo Documento">
	    		<% if(documento.getTipoDocumento().equals("TD01")){ %>
	    			<p align="center">Fattura</p>
	    		<%}else if(documento.getTipoDocumento().equals("TD02")){ %>
	    			<p align="center">Acconto/anticipo su fattura</p>
	    		<%}else if(documento.getTipoDocumento().equals("TD03")){ %>
	    			<p align="center">Acconto/anticipo su parcella</p>
    			<%}else if(documento.getTipoDocumento().equals("TD04")){ %>
	    			<p align="center">Nota di credito</p>
	    		<%}else if(documento.getTipoDocumento().equals("TD05")){ %>
	    			<p align="center">Nota di debito</p>
	    		<%}else if(documento.getTipoDocumento().equals("TD06")){ %>
	    			<p align="center">Parcella</p>
	    		<% } %>
	    	</liferay-ui:search-container-column-text>
	    	<liferay-ui:search-container-column-text name="Stato">
	    		<% if(documento.getStatoDocumento().equals("BOZZA")){ %>
	    			<p align="center" class="label workflow-status-draft label-info workflow-value"><%= documento.getStatoDocumento() %></p>
	    		<% }else if(documento.getStatoDocumento().equals("TRASMESSO")){ %>
	    			<p align="center" class="label workflow-status-approved label-success workflow-value"><%= documento.getStatoDocumento() %></p>
	    		<% } %>	
	    	</liferay-ui:search-container-column-text>

			<liferay-ui:search-container-column-text name="Dati Pagamento" >
       		 	<p align="center" ><a href="<portlet:renderURL><portlet:param name="documentoId" value="<%= String.valueOf(documento.getDocumentoId()) %>"/><portlet:param name="scelta" value="PAGAMENTO"/></portlet:renderURL>"><img src="<%= request.getContextPath() %>/img/pay.png" width="30px" /></a></p> 
       		</liferay-ui:search-container-column-text>
			
			<liferay-portlet:actionURL var="salvaDocumentoURL" name="creaDocumentoXml" portletName='fatturazionepa_WAR_fatturazionepaportlet'>
				<liferay-portlet:param name='documentoId' value="<%= String.valueOf(documento.getDocumentoId()) %>" />
			</liferay-portlet:actionURL>
	    	
	    	<liferay-ui:search-container-column-text name="Crea Documento XML" >
	    		<% if(DocumentoLocalServiceUtil.controlloDocumentoValido(documento.getDocumentoId())){ %>
	    			<p align="center" ><a href="<%= salvaDocumentoURL %>"><img src="<%= request.getContextPath() %>/img/xml.png" width="30px" /></a></p>
	    		<% }else{ %>
	    			<p style="color: red;">Ci sono ancora campi obbligatori vuoti.</p>
	    		<% } %>
	    	</liferay-ui:search-container-column-text>
	
	    	<liferay-ui:search-container-column-text name="Documento PDF" >
	    		<p align="center" ><img src="<%= request.getContextPath() %>/img/pdf.png" width="30px"/></p>
	    	</liferay-ui:search-container-column-text>
	    	
	    </liferay-ui:search-container-row>
	    <liferay-ui:search-iterator searchContainer="<%= searchContainer %>" />
	</liferay-ui:search-container>
</form>